package nl.haemers.components.grid 
{
	import flash.errors.IllegalOperationError;
	import flash.geom.Point;
	import nl.haemers.components.layout.ILayoutManager;
	/**
	 * ...
	 * @author Jonathan Haemers
	 */
	/*
	 * TODO: 
	 * - extend the grid with build direction. For now only left to right.
	 */ 
	public class Grid 
	{
		private var _layoutManager						:ILayoutManager;
		
		private var _columns							:uint = 0;
		//private var _rows								:uint = 0;
		
		public function Grid() 
		{
			
		}
		
		/** Public methods **/
		public function generateGrid(items:Vector.<GridItem>) : Vector.<GridItem>
		{
			if (!_layoutManager)
			{
				throw new IllegalOperationError("Don't have a LayoutManager!");
			}
			if (_columns/* + _rows */< 1)
			{
				throw new IllegalOperationError("You need to specify at least the amount of columns or the amount of rows.");
			}
			
			var item:GridItem;
			var counter:uint = 0;
			var columnCounter:int = 0;
			var rowCounter:int = 0;
			for each(item in items)
			{
				item.gridPosition = new Point(columnCounter, rowCounter);
				item.gridId = counter;
				
				if (columnCounter < _columns)
				{
					columnCounter++;
				}
				else
				{
					columnCounter = 0;
					rowCounter++;
				}
				
				counter++;
			}
			
			_layoutManager.createMap(items);
			
			return items;
		}
		
		/** Getters & Setters **/
		public function get layoutManager() : ILayoutManager
		{
			return _layoutManager;
		}
		public function set layoutManager(value:ILayoutManager) : void 
		{
			_layoutManager = value;
		}
		
		public function get columns() : uint
		{
			return _columns;
		}
		public function set columns(value:uint) : void 
		{
			_columns = value;
		}
		
		/*
		public function get rows() : uint
		{
			return _rows;
		}
		public function set rows(value:uint) : void 
		{
			_rows = value;
		}
		*/
		/** Protected methods **/
		
		/** Private methods **/
		
	}

}